package com.itheima.reggie.controller;

import com.itheima.reggie.common.PageResult;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


/*
* 员工管理控制层
* */
@RestController //@Controller*@ResponseBody(JSON处理 请求响应json {name:"xiaohong","age":18})
@RequestMapping("/employee")  //路径配置
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /*
    * 登录功能
    * */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        return employeeService.login(employee,session);
    }

    /*
    * 登出功能
    *
    * HttpSession session 内置对象
    * */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        return employeeService.logout(session);
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpSession session){
        return employeeService.save(employee,session);
    }

    /**
     * 员工分页
     *
     * @RequestParam(value = "page",defaultValue = "1") Long page
     * value = "page":名称必须跟页面传入参数名一致
     * defaultValue = "1":默认当前页面
     * Long page：名称自定义
     */
    @GetMapping("/page")
    public R<PageResult> findPage(@RequestParam(value = "page", defaultValue = "1") Long page, @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize,String name) {
        return employeeService.findPage(page, pageSize,name);
    }

    @PutMapping
    public R<String> update(HttpSession session,@RequestBody Employee employee){
        long id = Thread.currentThread().getId();
        log.debug("*****************EmployeeController************线程id为{}**",id);
        return employeeService.update(employee,session);
    }

    /**
     * 根据员工id查询员工对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }
}
